package com.lmaye.eshop.inventory.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lmaye.eshop.inventory.dao.RedisDao;
import com.lmaye.eshop.inventory.entity.User;
import com.lmaye.eshop.inventory.mapper.UserMapper;
import com.lmaye.eshop.inventory.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmay.Zhou
 * @since 2019-03-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * RedisDao
     */
    @Resource
    private RedisDao redisDao;

    /**
     * 查询用户信息
     * - 根据用户名
     *
     * @param name 用户名
     * @return User
     */
    @Override
    public User getByName(String name) {
        return baseMapper.selectByName(name);
    }

    /**
     * 获取缓存用户信息
     *
     * @return User
     */
    @Override
    public User getCachedUserInfo() {
        redisDao.set("cached_user_test", "{\"name\": \"test\", \"age\": \"28\"}");

        String userJson = redisDao.get("cached_user_test");
        JSONObject userJsonObject = JSONObject.parseObject(userJson);

        User user = new User();
        user.setName(userJsonObject.getString("name"));
        user.setAge(userJsonObject.getInteger("age"));
        return user;
    }
}
